import 'dart:math';

import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/toast.dart';
import '../home/api/home_api.dart';
import '../home/bean/popUpsBean.dart';
import '../login_page/constant.dart';
import '../mine/api/mine_api.dart';
import '../mine/bean/aboutUsBean.dart';


class ProductBottomLogic extends GetxController {
  int randomElement=10000;
  late bool isAgreeUserPrivary=false;
  List<PopUpsInfo> popUpsInfo=[];
  String? imgUrl='';
  num? amount=0;
  String? content='';
  String? popLinkUrl='';
  num? id=0;
  @override
  void onInit() {
    super.onInit();
    List<int> givenList =[10000,20000,30000,35000];
    Random rand =  Random();
    randomElement = givenList[rand.nextInt(givenList.length)];
    getCpaPopList();
  }
  //用户隐私协议选中或未选中
  void updateUserPrivary() {
    isAgreeUserPrivary = !isAgreeUserPrivary;
    update();
  }
  //信用授权书
  getUserInfo(int type) {
    MineApi.getHTML(
        type,
        BaseCallback(onSuccess: (map) {
          AboutUsBean aboutUsBean = AboutUsBean.fromJson(map);
          if (aboutUsBean.data != null) {
            String? agreementName = aboutUsBean.data?.name;
            String? agreementContent = aboutUsBean.data?.content;
            Get.toNamed(BaseRouterConfig.agreement, arguments: {'name': '', 'content': agreementContent});
          }
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
          });
        }));
  }

  //弹窗
  void getCpaPopList() async {
    HomeApi.getCpaPopList(
        PopUps().audit,
        BaseCallback(
            onSuccess: (map) {
              PopUpsBean popUpsBean = PopUpsBean.fromJson(map);
              popUpsInfo = popUpsBean.data!;
              if (popUpsInfo.isNotEmpty) {
                imgUrl = popUpsInfo[0].imgUrl;
                amount = popUpsInfo[0].amount;
                content = popUpsInfo[0].name;
                popLinkUrl = popUpsInfo[0].linkUrl;
                id = popUpsInfo[0].id;
                update();
              }
            },
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }

  //埋点
  void addLoanRecord(num productId, int showPage) async {
    HomeApi.addLoanRecord(
        productId,
        showPage,
        BaseCallback(
            onSuccess: (map) {},
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }
}